using UnityEngine;
using System.Collections;

[System.Serializable]
public class Event2Redesigned : MonoBehaviour {
	
	public static int ANXIETY_LEVEL_ADD = 1;
	
	public string attackAnimation;
	public string idleAnimation;
	public string walkAnimation;
	
	public MonsterMove monsterMoveScript;
	public GameObject player;
	public float minDistanceToAttack = 2.0f;
	
	public string playerTag;
	
	private bool triggered = false;
	private bool attacking = false;
	private float startTime = -1f;
	private bool monsterIsCloseToPlayer = false;
	
	public AudioSource monsterMouth;
	public AudioClip monsterGrowl;
	
	public AudioSource playerHearth;
	public AudioClip heartbeat;
	public AudioSource playerMouth;
	public AudioClip breathingModerate;
	public AudioClip breathingFast;
	public AudioClip breathingVeryFast;
		
	public float secondsBetweenAttacks = 1.1f;
	
	public bool changeAnimationSpeed = false;
	
	public NavPoint playerNavPoint;
	
	public SkinnedMeshRenderer monster;
	public ParticleEmitter[] particlesWhenPoof;
	public Light particleGlowWhenPoof;
	public float waitSecondsBeforeVanish = 1.1f;
	
	private float oldSpeed = 1.0f;
	
	private bool vanished = false;
	
	// Use this for initialization
	void Start () {	
		oldSpeed = monsterMoveScript.speed;
		if (particlesWhenPoof != null) {
			foreach (ParticleEmitter ps in particlesWhenPoof) {
				ps.enabled = false;
			}
		}
		
		if (particleGlowWhenPoof != null) {
			particleGlowWhenPoof.enabled = false;
		}
			
	}
	
	// Update is called once per frame
	void Update () {
		if (triggered) {
			float distance = Vector3.Distance(player.transform.position, monsterMoveScript.monster.transform.position);
			
			Debug.Log("Distance to player: " + distance);
						
			if (distance <= minDistanceToAttack && !monsterIsCloseToPlayer) {
				Debug.Log("Monster is close!");
				monsterIsCloseToPlayer = true;
				monsterMoveScript.triggered = false;
				monsterMoveScript.monster.animation.Play(idleAnimation);
				startTime = Time.time;
			} else if (Game.game.isPlayerInsideSafePlace) {
				monsterIsCloseToPlayer = false;
				monsterMoveScript.nextNavPoint = Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe;
				if (Vector3.Distance(monsterMoveScript.monster.transform.position, 
									 Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.transform.position) 
					> Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.threshold) {
					monsterMoveScript.rotateTowards.target = Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.transform;
				} else {
					monsterMoveScript.triggered = false;
					monsterMoveScript.monster.animation.Play(idleAnimation);
					monsterMoveScript.rotateTowards.target = player.transform;
				}
				vanished = true;
			} else {
				monsterIsCloseToPlayer = false;
			}
			if (!Game.game.isPlayerInsideSafePlace && !monsterIsCloseToPlayer) {	
				if (!vanished) {
					monsterMoveScript.monster.animation.Play(walkAnimation);
					monsterMoveScript.rotateTowards.target = player.transform;
					monsterMoveScript.nextNavPoint = playerNavPoint;
					monsterMoveScript.triggered = true;
				} else {
					if (distance > 35.2f) {
						vanished = false;
						monster.renderer.enabled = true;
						monsterMouth.Play();
					} else {
						if (particlesWhenPoof != null) {
							foreach (ParticleEmitter ps in particlesWhenPoof) {
								ps.enabled = true;
							}
						}					
						if (particleGlowWhenPoof != null) {
							particleGlowWhenPoof.enabled = true;
						}
					}
				}
			}
		}		
			
		if (monsterIsCloseToPlayer) {
			if (!attacking) {
				attacking = true;
				monsterMoveScript.monster.animation.Play(attackAnimation);
				playerMouth.clip = breathingVeryFast;
				playerHearth.clip = heartbeat;
				if (!playerMouth.isPlaying) {
					playerMouth.Play();
				}
				if (!playerHearth.isPlaying) {
					playerHearth.Play();
				}
				Game.game.anxietyLevel += ANXIETY_LEVEL_ADD;
			} 			
			
			float currentTime = Time.time;
				
			Debug.Log("Time waited: " + (currentTime-startTime) + ", Start time: " + startTime + ", Current Time: " + currentTime);
			
			if (currentTime-startTime >= waitSecondsBeforeVanish) {
				monster.renderer.enabled = false;
				monsterMouth.Stop();
				monsterIsCloseToPlayer = false;
				monsterMouth.Stop();
						
				if (particlesWhenPoof != null) {
					foreach (ParticleEmitter ps in particlesWhenPoof) {
						ps.enabled = false;
					}
				}					
				if (particleGlowWhenPoof != null) {
					particleGlowWhenPoof.enabled = false;
				}
			}
			
			if (currentTime-startTime >= secondsBetweenAttacks) {
				attacking = false;
			}
		}
	}
	
	void OnTriggerEnter(Collider collision) {
		if (collision.tag == playerTag && !triggered) {
			doTrigger();
        }
	}
	
	void doTrigger() {
		Debug.Log("Event triggered!");
		
		if (monsterMoveScript != null) {
			float newSpeed = monsterMoveScript.startSpeed * 2.2f;				
			monsterMoveScript.speed = newSpeed;
			
			if (changeAnimationSpeed) {
				monsterMoveScript.monster.animation[walkAnimation].speed = newSpeed;
			}
			if (monsterMouth != null) {
				monsterMouth.enabled = true;
				monsterMouth.Play();
			}
		}
		monsterMoveScript.triggered = true;
		monsterMoveScript.monster.animation.Play(walkAnimation);
		triggered = true;
		//characterMotor.canControl = false;
	}
}
